package com.initialize.service.system.controller;

import com.initialize.common.mybatis.plus.entity.Role;
import com.initialize.common.mybatis.plus.entity.User;
import com.initialize.common.mybatis.plus.mapper.RoleMapper;
import com.initialize.common.mybatis.plus.service.impl.RoleServiceImpl;
import com.initialize.common.mybatis.plus.service.impl.UserServiceImpl;
import com.initialize.service.api.system.entity.UserInfo;
import com.initialize.service.system.service.UserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

/**
 * @program: Initialization_Project
 * @description: 用户Controller
 * @author: Mr.Jane
 * @create: 2019-10-10 22:25
 **/
@Api(tags = "用户API")
@Slf4j
@RestController
@RequestMapping("user")
public class UserController {

    final
    UserService userService;

    final
    UserServiceImpl userServiceImpl;


    @Value("${server.port}")
    String port;

    final
    RoleServiceImpl roleService;

    public UserController(UserService userService,
                          UserServiceImpl userServiceImpl,
                          RoleServiceImpl roleService) {
        this.userService = userService;
        this.userServiceImpl = userServiceImpl;
        this.roleService = roleService;
    }


    @Autowired
    RoleMapper roleMapper;


    @ApiOperation(value = "客户端--用户登录", notes = "用户登录")
    @PostMapping("/findByUsername")
    public UserInfo findByUsername(@RequestBody String username) {
        User user = userService.selectLogin(username);
        List<Role> roles = new ArrayList<>();
        Role byId = roleMapper.selectById(user.getRoleId());
        roles.add(byId);
        return UserInfo.builder()
                .password(user.getUPsd())
                .userName(user.getULogin())
                .roles(roles)
                .build();

    }

    @GetMapping("/port")
    public String getPort() {
        return port;
    }
}
